$(function () {
	const getArticleCate = () => {
		$.ajax({
			url: '/my/article/cates',
			method: 'get',
			success: res => {
				if (res.status != 0) return layer.msg(res.message);
				let rows = [];
				$.each(res.data, function (i, item) {
					rows.push(`
						<tr>
							<td>${item.name}</td>
							<td>${item.alias}</td>
							<td>
								<button type="button" class="layui-btn layui-btn-xs edit" data-id="${item.Id}">编辑</button>
								<button type="button" class="layui-btn layui-btn-xs layui-btn-danger del" data-id="${item.Id}">删除</button>
							</td>
						</tr>
					`);
				});
				$('.article tbody').empty().append(rows.join(''));
			}
		});
	}
	getArticleCate();

	let indexAdd = null;
	$('.add').on('click', function () {
		indexAdd = layer.open({
			type: 1,
			area: ['500px', '250px'],
			title: '添加文章分类',
			content: $('#dialog-add').html()
		});
	});

	$('body').on('submit', '#form-add', function (e) {
		e.preventDefault();
		$.ajax({
			url: '/my/article/addcates',
			method: 'post',
			data: $(this).serialize(),
			success: res => {
				if (res.status != 0) return layer.msg(res.message);
				layer.msg(res.message);
				getArticleCate();
				layer.close(indexAdd);
			}
		});
	});

	let indexEdit = null;
	let form = layui.form;
	$('.article tbody').on('click', '.edit', function () {
		$.ajax({
			url: '/my/article/cates/' + $(this).attr('data-id'),
			method: 'get',
			success: res => {
				if (res.status != 0) return layer.msg(res.message);
				form.val('form-edit', res.data);
			}
		});
		indexEdit = layer.open({
			type: 1,
			area: ['500px', '250px'],
			title: '编辑文章分类',
			content: $('#dialog-edit').html()
		});
	});

	$('body').on('submit', '#form-edit', function (e) {
		e.preventDefault();
		$.ajax({
			url: '/my/article/updatecate',
			method: 'post',
			data: $(this).serialize(),
			success: res => {
				if (res.status != 0) return layer.msg(res.message);
				layer.msg(res.message);
				getArticleCate();
				layer.close(indexEdit);
			}
		});
	});

	$('.article tbody').on('click', '.del', function () {
		layer.confirm('确定删除? ', { 'icon': 3, 'title': '提示' }, index => {
			$.ajax({
				url: '/my/article/deletecate/' + $(this).attr('data-id'),
				method: 'get',
				success: res => {
					if (res.status != 0) return layer.msg(res.message);
					layer.msg(res.message);
					getArticleCate();
					layer.close(index);
				}
			});
		});
	});
});